package com.farm.kaiping.controller;


import com.farm.kaiping.entity.SysRegion;
import com.farm.kaiping.po.SysRegionPO;
import com.farm.kaiping.service.SysRegionService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.farm.common.util.PageParaUtils;
import com.farm.common.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 行政区划(SysRegion)表控制层
 *
 * @author zhuhui
 * @since 2024-11-26 22:58:21
 */
@RestController
@RequestMapping("/sysRegion")
@AllArgsConstructor
@Api(tags = "行政区划控制器")
public class SysRegionController {

	private final SysRegionService sysRegionService;

	@ApiOperation(value = "分页查询行政区划", notes = "权限：view_sysRegion_page")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_sysRegion_page')")
	@PostMapping("/page")
	public R<Page<SysRegion>> getSysRegionPage(@RequestBody SysRegionPO sysRegionPO) {
		try {
			Page page = PageParaUtils.withdrawPageByParamType(sysRegionPO);
			return R.ok(sysRegionService.getSysRegionPage(page, sysRegionPO));
		} catch (Exception e) {
			return R.failed(e.getMessage());
		}
	}

	@ApiOperation(value = "查询行政区划列表", notes = "权限：view_sysRegion_list")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_sysRegion_list')")
	@PostMapping("/list")
	public R<List<SysRegion>> getSysRegionList(@RequestBody SysRegionPO sysRegionPO) {
		try {
			return R.ok(sysRegionService.getSysRegionList(sysRegionPO));
		} catch (Exception e) {
			return R.failed(e.getMessage());
		}
	}

	@ApiOperation(value = "通过id查询行政区划", notes = "权限 view_sysRegion_view")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_sysRegion_view')")
	public R getById(@PathVariable("id") String id) {
		try {
			return R.ok(sysRegionService.getById(id));
		} catch (Exception e) {
			return R.failed(e.getMessage());
		}
	}

	@ApiOperation(value = "查询一条行政区划", notes = "权限：view_sysRegion_one")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_sysRegion_one')")
	@PostMapping("/one")
	public R<SysRegion> getSysRegion(@RequestBody SysRegionPO sysRegionPO) {
		try {
			return R.ok(sysRegionService.getSysRegion(sysRegionPO));
		} catch (Exception e) {
			return R.failed(e.getMessage());
		}
	}

	@ApiOperation(value = "新增行政区划", notes = "权限：view_sysRegion_add")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_sysRegion_add')")
	@PostMapping("/add")
	public R saveSysRegion(@RequestBody SysRegionPO sysRegionPO) {
		try {
			return R.ok(sysRegionService.saveSysRegion(sysRegionPO));
		} catch (Exception e) {
			return R.failed(e.getMessage());
		}
	}

	@ApiOperation(value = "修改行政区划", notes = "权限：view_sysRegion_edit")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_sysRegion_edit')")
	@PostMapping("/edit")
	public R updateSysRegion(@RequestBody SysRegionPO sysRegionPO) {
		try {
			return R.ok(sysRegionService.updateSysRegion(sysRegionPO));
		} catch (Exception e) {
			return R.failed(e.getMessage());
		}
	}

	@ApiOperation(value = "通过id删除行政区划", notes = "权限：view_sysRegion_del")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_sysRegion_del')")
	@PostMapping("/del")
	public R removeSysRegion(@RequestBody SysRegionPO sysRegionPO) {
		try {
			return R.ok(sysRegionService.removeSysRegion(sysRegionPO));
		} catch (Exception e) {
			return R.failed(e.getMessage());
		}
	}

	@ApiOperation(value = "根据条件获取行政区划树形结构列表", notes = "权限：view_sysRegion_tree")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_sysRegion_tree')")
	@PostMapping("/tree")
	public R treeSysRegion(@RequestBody SysRegionPO sysRegionPO) {
		try {
			return R.ok(sysRegionService.treeSysRegion(sysRegionPO));
		} catch (Exception e) {
			return R.failed(e.getMessage());
		}
	}


}

